clear
clc

algoCandi = {'Prostate_GE_102n_5966d_2c_12k'};

for i_dataset = 1: length(algoCandi)
    dataName = algoCandi{i_dataset};
    load(dataName, 'Ks', 'Y');
    disp(['algorithm = ', dataName, '......']);
    
    if size(Y, 1) == 1
        Y = Y';
    end
    
    [nSmp, ~, nKernel] = size(Ks);
    nCluster = length(unique(Y));
    % Ks normalization
    Ks = kcenter(Ks);
    Ks = knorm(Ks);
    
    % setting parameter
    lambda = 1;
    mid_layer_clusters1 = (3:10) * nCluster;
    mid_layer_clusters2 = (2:5) * nCluster;
    mid_layer_dropout_ratio = (0.5:0.1:0.8);
%     mid_layer_dropout_ratio = (0);
    mid_layer_width1 = (8:2:14);
    mid_layer_width2 = (6:2:10);
    
    % parameter array
    paramCount = 1;
    nIter = 10;
    for i1 = 1:length(mid_layer_clusters1)
        for i2 = 1:length(mid_layer_clusters2)
            for i3 = 1:length(mid_layer_width1)
                for i4 = 1:length(mid_layer_width2)
                    for i5 = 1:length(mid_layer_dropout_ratio)
                        if mid_layer_clusters1(i1) > mid_layer_clusters2(i2)
                            paramCandi(paramCount, 1) = mid_layer_clusters1(i1); %#ok
                            paramCandi(paramCount, 2) = mid_layer_clusters2(i2); %#ok
                            paramCandi(paramCount, 3) = mid_layer_width1(i3); %#ok
                            paramCandi(paramCount, 4) = mid_layer_width2(i4); %#ok
                            paramCandi(paramCount, 5) = mid_layer_dropout_ratio(i5); %#ok
                            paramCount = paramCount + 1;
                        end
                    end
                end
            end
        end
    end
    paramLen = size(paramCandi, 1);
    
    fname = fullfile(['F:\Need\dropHMKC\MKC-2021\MKC-2021\dropoutHMKCEXP_PLOT\', dataName '_SCHMKKM.mat']);
    if exist(fname, 'file')
        load(fname);
    else
        
        % save iIter res
        resIter = zeros(nIter, 10);
        timeElapsedIter = zeros(nIter, 1);
        objHistoryIter = cell(nIter, 1);
        % save nIter res
        res_nIter = cell(nIter, 1);
        timeElapsed_nIter = cell(nIter, 1);
        objHistory_nIter = cell(nIter, 1);
        % save best parameter
        bestParam = zeros(nIter, 5);
        for iIter = 1:nIter
            disp(['iIter = ', num2str(iIter), ' nIter = ', num2str(nIter)]);
            res_nParam = zeros(paramLen, 10);
            timeElapsed_nParam = zeros(paramLen, 1);
            objHistory_nParam = cell(paramLen, 1);
            for iParam = 1:paramLen
                disp(['iParma = ', num2str(iParam), ' nParam = ', num2str(paramLen)]);
                param1 = paramCandi(iParam, 1);
                param2 = paramCandi(iParam, 2);
                param3 = paramCandi(iParam, 3);
                param4 = paramCandi(iParam, 4);
                param5 = paramCandi(iParam, 5);
                tic;
                [H_normalized, HPs, beta, gamma, lambda, obj] = ...
                    dropoutHMKC(Ks, lambda, nCluster, [param1, param2], [param3, param4], param5);
                timeElapsed_nParam(iParam, 1) = toc;
                ytmp = my_lite_kmeans(H_normalized, nCluster);
                res_nParam(iParam, :) = my_eval_y(ytmp, Y);
                objHistory_nParam{iParam, 1} = obj;
                fprintf('time: %f, dr_ratio: %.3f layer_width: [%d %d] layer1 c1: %d, layer2 c2: %d \n', timeElapsed_nParam(iParam, 1), param5, [param3, param4], param1/nCluster, param2/nCluster);
                fprintf('acc: %f, nmi: %f, ari: %f\n', res_nParam(iParam, 1), res_nParam(iParam, 2), res_nParam(iParam, 5));
         
            end
            % save best res in all parameter
            [~, best_idx] = max(res_nParam, [], 1);
            modeIdx = mode(best_idx);
            resIter(iIter, :) = res_nParam(modeIdx, :);
            timeElapsedIter(iIter, 1) = timeElapsed_nParam(modeIdx, 1);
            objHistoryIter{iIter, 1} = objHistory_nParam{modeIdx, 1};
            bestParam(iIter, :) = paramCandi(modeIdx, :);
            % save all res
            res_nIter{iIter, 1} = res_nParam;
            timeElapsed_nIter{iIter, 1} = timeElapsed_nParam;
            objHistory_nIter{iIter, 1} = objHistory_nParam;
        end
        resMax = max(resIter, [], 1); % max res
        resMean = mean(resIter, 1); % mean res
        resVar = var(resIter, 1, 1);% var res
        % save resAIO
        resAIO{1} = resMax;
        resAIO{2} = resMean;
        resAIO{3} = resVar;
        resAIO{4} = resIter;
        resAIO{5} = timeElapsedIter;
        resAIO{6} = objHistoryIter;
        resAIO{7} = bestParam;
        resAIO{8} = res_nIter;
        resAIO{9} = timeElapsed_nIter;
        resAIO{10} = objHistory_nIter;
        save(fname, 'resAIO');
        disp(['algo = ', dataName, ' .........done..........']);
    end
end